package com.rattsoftware.client.views;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.RootPanel;
import com.rattsoftware.client.events.AppEvents;

public class MainAppView extends View{

	public static final String WEST_PANEL = "west";
	public static final String VIEWPORT = "viewport";
	public static final String CENTER_PANEL = "center";

	private Viewport viewport;
	private ContentPanel west;
	private LayoutContainer center;
	
	public MainAppView(Controller controller) {
		super(controller);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		}
	}
	
	private void initUI() {
		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());

		createNorth();
		createSouth();
		createWest();
		createCenter();

		// registry serves as a global context
		Registry.register(VIEWPORT, viewport);
		Registry.register(WEST_PANEL, west);
		Registry.register(CENTER_PANEL, center);

		RootPanel.get().add(viewport);
	}

	private void createNorth() {
		
		Html signOut = new Html("<div id='demo-header' class='x-small-editor'><div id='demo-theme'></div><div id=demo-title>Resource Allocation &" +
				" Time Tabling System. <div id=div-logout><a href=\"\">LogOut</a></div></div> </div>");
		
		StringBuffer sb = new StringBuffer();
		//sb.append("<div id='demo-header' class='x-small-editor'><div id='demo-theme'></div><div id=demo-title>Resource Allocation & Time Tabling System. <div id=div-logout>LogOut</div></div> </div>");

		sb.append("<div id='demo-header' class='x-small-editor'><div id='demo-theme'></div><div id=demo-title>Resource Allocation &" +
		" Time Tabling System. <div id=div-logout><a href=\"\">LogOut</a></div></div> </div>");
		
		HtmlContainer northPanel = new HtmlContainer(sb.toString());
		northPanel.setStateful(false);

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 33);
		data.setMargins(new Margins());
		viewport.add(northPanel, data);
	}

	private void createSouth() {
		StringBuffer sb = new StringBuffer();
		sb
				.append("<div id='div-south' class='x-small-editor'><div id='div-theme'></div><div id=div-footer>Copy Right 2011- 2012.</div></div>");

		HtmlContainer southPanel = new HtmlContainer(sb.toString());
		southPanel.setStateful(false);

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.SOUTH, 33);
		data.setMargins(new Margins());
		viewport.add(southPanel, data);
	}
	
	private void createWest() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 200,
				150, 350);
		data.setMargins(new Margins(5, 0, 5, 5));

		west = new ContentPanel();
		west.setBodyBorder(false);
		west.setLayout(new AccordionLayout());
		west.setLayoutOnChange(true);
		west.setHeading("Res-Man");

		viewport.add(west, data);
	}

	private void createCenter() {
		center = new LayoutContainer();
		center.setLayout(new FitLayout());

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		data.setMargins(new Margins(5, 5, 5, 5));

		viewport.add(center, data);
	}
}
